/*
 * Copyright (C) 2012-2013  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.util.swing;

import javax.swing.table.AbstractTableModel;

/**
 * DynamicTableModel : The dynamic table model.
 * 
 * @param <R>
 *            The row object type
 */
public abstract class DynamicTableModel<R> extends AbstractTableModel {

    /** Serial version */
    private static final long serialVersionUID = 1L;

    /** The row converter */
    private final IRowConverter<R> mConverter;

    /**
     * Constructor.
     * 
     * @param pConverter
     *            The row converter
     */
    public DynamicTableModel(final IRowConverter<R> pConverter) {
        super();

        mConverter = pConverter;
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.swing.table.TableModel#getColumnCount()
     */
    @Override
    public int getColumnCount() {
        return mConverter.getColumnCount();
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
     */
    @Override
    public Class<?> getColumnClass(final int pColumnIndex) {
        return mConverter.getColumnType(pColumnIndex);
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.swing.table.TableModel#getValueAt(int, int)
     */
    @Override
    public Object getValueAt(final int pRowIndex, final int pColumnIndex) {
        return mConverter.getValue(getRowValue(pRowIndex), pColumnIndex);
    }

    /**
     * Get the required row value.
     * 
     * @param pRowIndex
     *            The required row index
     * @return The row value
     */
    protected abstract R getRowValue(final int pRowIndex);
}
